// pages/csregistration/csregistration.js
var util = require("../../utils/util.js")
var api = require("../../utils/api.js")
var app = getApp();
const device = wx.getSystemInfoSync()
const W = device.windowWidth
const H = device.windowHeight - 50
let cropper = require('../../utils/welCropper/welCropper.js');
var a = false
Page({

    /**
     * 页面的初始数据
     */
    data: {
        whether: 0,
        infotype: 1,
        accountid: 0,
        logo: '../res/0010.png',
        period_ids: {},
        arealist: {}, //省
        citylist: {}, //省
        provincename: '请选择省', //省
        cityname: '请选择市', //省
        province_id: 0,
        city_id: 0,
        name: '',
        category_id:0,
        category_name:'请选择分类',
        address:'',
        worktime:'',
        price:0,
        pricename:'选择价格类型',
        phone:'',
        weixin:'',
        web_site:'',
        intro:'',
        organizationtype:{},
        pricelist:{},
        id:0,
      isread:0
    },
    nameInput: function(e) {
        this.setData({
            name: e.detail.value
        })
    },
    mytouchstart: function(e) {
        this.setData({
            touch_start: e.timeStamp
        })
    },
    mytouchend: function(e) {
        this.setData({
            touch_end: e.timeStamp
        })
    },
    addimg: function() {
        a = true
        api.chooseimage(9, this.data.qualificationphotos, 'file', app.u.HOME_URL + app.u.UPLOADIMG).then(res => {
            this.setData({
                qualificationphotos: this.data.qualificationphotos.concat(res)
            })
        })
    },
    giftview: function(e) {
        a = true
        let touchTime = this.data.touch_end - this.data.touch_start
        if (touchTime > 350) {
            let that = this
            let images = that.data.qualificationphotos
            var index = e.currentTarget.dataset.index
            wx.showModal({
                title: '提示',
                content: '确定要删除此图片吗？',
                success(res) {
                    if (res.confirm) {
                        images.splice(index, 1)
                        that.setData({
                            qualificationphotos: images
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            wx.previewImage({
                current: e.currentTarget.dataset.url,
                urls: this.data.qualificationphotos
            })
        }
    },
    bindProvinceChange: function(e) {
        this.setData({
            province_id: this.data.arealist[e.detail.value].area_id,
            provincename: this.data.arealist[e.detail.value].area_name,
        })
        console.log(this.data.arealist[e.detail.value].area_id)
        this.getarealist()
    },
    bindOrgChange(e){
        this.setData({
            category_id: this.data.organizationtype[e.detail.value].id,
          category_name:this.data.organizationtype[e.detail.value].name,
        })
    } ,
    
    bindPriceChange(e){
        this.setData({
            price: this.data.pricelist[e.detail.value].id,
          pricename:this.data.pricelist[e.detail.value].name,
        })
    },
    getarealist: function() {
        console.log(11111111111111)
        let url3 = app.u.HOME_URL + app.u.GET_AREA_LIST
        let data3 = { province: this.data.province_id }
        api.requestUrl(data3, url3).then(res => {
            if (res.code == 200) {
                this.setData({
                    city_id: res.data[0].area_id,
                    cityname: res.data[0].area_name
                })
                this.setData({
                    citylist: res.data
                })
            } else {
                api.showError(res.msg)
            }
        })
    },
    bindCityChange: function(e) {
        this.setData({
            city_id: this.data.citylist[e.detail.value].area_id,
            cityname: this.data.citylist[e.detail.value].area_name
        })
    },
    // bindPickerChange: function(e) {
    //     this.setData({
    //         qualification: this.data.tag_list[e.detail.value].id,
    //         qualificationtext: this.data.tag_list[e.detail.value].name
    //     })
    // },
    addressInput: function(e) {
        this.setData({
            address: e.detail.value
        })
    },
    priceInput: function(e) {
        this.setData({
            price: e.detail.value
        })
    },
    worktimeInput: function(e) {
        this.setData({
            worktime: e.detail.value
        })
    },
    phoneInput: function(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    weixinInput: function(e) {
        this.setData({
            weixin: e.detail.value
        })
    },
    web_siteInput: function(e) {
        this.setData({
            web_site: e.detail.value
        })
    },
    introInput: function(e) {
        this.setData({
            intro: e.detail.value
        })
    },
    selectTap(e) {
        a = true
        let that = this
        let mode = e.currentTarget.dataset.mode

        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success(res) {
                const tempFilePath = res.tempFilePaths[0]
                console.log(tempFilePath)

                // 将选取图片传入cropper，并显示cropper
                // mode=rectangle 返回图片path
                // mode=quadrangle 返回4个点的坐标，并不返回图片。这个模式需要配合后台使用，用于perspective correction
                // let modes = ["rectangle", "quadrangle"]
                // let mode = modes[1]   //rectangle, quadrangle
                that.showCropper({
                    src: tempFilePath,
                    mode: mode,
                    sizeType: ['original', 'compressed'], //'original'(default) | 'compressed'
                    maxLength: 1000, //默认2000，允许最大长宽，避免分辨率过大导致崩溃
                    callback: (res) => {
                        if (mode == 'rectangle') {
                            wx.uploadFile({
                                url: app.u.HOME_URL + app.u.UPLOADIMG, //仅为示例，非真实的接口地址
                                filePath: res,
                                name: 'file',
                                formData: {
                                    user: 'test'
                                },
                                success(res) {
                                    that.setData({
                                        logo: res.data
                                    })

                                }
                            })
                            that.hideCropper()
                        } else {
                            wx.showModal({
                                title: '',
                                content: JSON.stringify(res),
                            })
                        }
                        // that.hideCropper() //隐藏，我在项目里是点击完成就上传，所以如果回调是上传，那么隐藏掉就行了，不用previewImage
                    }
                })
            }
        })
    },
    changeheadimg: function() {
        a = true
        let that = this
        wx.chooseImage({
            success(res) {
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: app.u.HOME_URL + app.u.UPLOADIMG, //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        'user': 'test'
                    },
                    success(res) {
                        that.setData({
                            logo: res.data
                            })
                            //do something
                    }
                })
            }
        })
    },
    updateinfo: function() {
        let that = this
        if (that.data.logo === '../res/0010.png') {
            api.showError('请上传头像')
            return false
        }
        if (that.data.name === '') {
            api.showError('请填写组织名')
            return false
        }
        if (that.data.category_id === 0) {
            api.showError('请选择组织类别')
            return false
        }
        if (that.data.province_id === 0) {
            api.showError('请选择省份')
            return false
        }
        if (that.data.city_id === 0) {
            api.showError('请选择城市')
            return false
        }
      if (that.data.isread === 0) {
        api.showError('请先阅读协议')
        return false
      }
       
       
        if (that.data.address === '') {
            api.showError('请填写详细地址')
            return false
        }
        if (that.data.worktime === '') {
            api.showError('请填写工作时间段')
            return false
        }
        if (that.data.price === '0') {
            api.showError('请选择咨询费用类型')
            return false
        }
        if (that.data.phone === '') {
            api.showError('请填写电话号码')
            return false
        }
        if (that.data.weixin === '') {
            api.showError('请填写微信号')
            return false
        }
        if (that.data.web_site === '') {
            api.showError('请填写网站链接')
            return false
        }
       
        let data = {
            accountid:that.data.accountid,
            id:that.data.id,
            name:that.data.name,
            category_id:that.data.category_id,
            intro:that.data.intro,
            price:that.data.price,
            province_id:that.data.province_id,
            city_id:that.data.city_id,
            address:that.data.address,
            phone:that.data.phone,
            weixin:that.data.weixin,
            web_site:that.data.web_site,
            logo:that.data.logo,
            worktime:that.data.worktime,
            isread:that.data.isread
        }

        let url = app.u.HOME_URL + app.u.ORGINFO
        api.requestUrl(data, url).then(res => {
            if (res.code == 200) {
                api.showSuccess(res.msg)
                wx.navigateBack({
                    delta: 1,
                })
            } else {
                api.showError(res.msg)
            }
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
        cropper.init.apply(that, [W, H]);
    },
    getarealistz: function() {
        let url3 = app.u.HOME_URL + app.u.GET_AREA_LIST
        let data3 = { province: this.data.province }
        api.requestUrl(data3, url3).then(res => {
            if (res.code == 200) {
                this.setData({
                    arealist: res.data
                })
            } else {
                api.showError(res.msg)
            }
        })
    },
    getorgtags(){
        let url3 = app.u.HOME_URL + app.u.GET_ORG_TAGS
        api.requestUrl({}, url3).then(res => {
            if (res.code == 200) {
                this.setData({
                    pricelist: res.data.psy_price,
                    organizationtype:res.data.organization
                })
            } else {
                api.showError(res.msg)
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        if (a) {
            a = false;
            return;
        }
        if (this.isFromShare) return;
        let that = this
        that.getorgtags()
        wx.getStorage({
            key: 'userInfo',
            success: function(acres) {
                console.log(acres)
                that.setData({
                    accountid:acres.data.id,
                    id:acres.data.org_id,
                })  
                if(acres.data.org_id){
                    let data = { id: acres.data.org_id }
                    
                    let url = app.u.HOME_URL + app.u.ORGEN_INFO
                    api.requestUrl(data, url).then(res => {
                        if (res.code == 200) {
                            that.setData({
                                name:res.data.name,
                                category_id:res.data.category_id,
                                category_name:res.data.category_name,
                                intro:res.data.intro,
                                price:res.data.price,
                                province_id:res.data.province_id,
                                provincename:res.data.province,
                                city_id:res.data.city_id,
                                cityname:res.data.city,
                                address:res.data.address,
                                phone:res.data.phone,
                                weixin:res.data.weixin,
                                web_site:res.data.web_site,
                                logo:res.data.logo,
                                worktime:res.data.worktime,
                                pricename:res.data.pricename,
                                isread: res.data.isread
                            })
                            that.getarealistz()
                        } else {
                            that.getarealistz()
                            api.showError(res.msg)
                        }
                    })
                }else{
                    that.getarealistz()
                }
                
            }
        })
    },
  radioChange(e) {
    this.setData({
      isread: 1
    })
  },
  isread(e) {
    wx.navigateTo({
      url: '/pages/thetext/thetext?id=160',
    })
  }
})